
<style type="text/css">
#codeparter_main { margin: 5px; }
#codeparter_main a { color: #000; text-decoration: underline; }
#codeparter_main a:hover { color: #000; text-decoration: underline overline; background: white; }
#codemananger table { border: 1px solid gray; border-collapse: collapse; }
#codemananger table th { width: 120px; }
#codemananger textarea { width: 600px; height: 200px; }
#codebutton { margin: 10px; }
</style>

<div id="codeparter_main">
	<h2>通用代码块管理</h2>
	<div id="codelist">
		<a href="/admin/codeparter">新建代码块</a> | 修改代码块：<% @codelist.each do |code| %><a href="/admin/codeparter?id=<%=code.id%>"><%=code.title%></a>&nbsp;<% end %>
	</div>
	<div id="codemananger">
		<h3><%=case when @codeinfo.id == nil then '新建' else '修改' end %>代码块</h3>
		<form>
			<table border="1" cellpadding="5">
				<tr>
					<th>名称：</th>
					<td><input type="text" id="name" value="<%=@codeinfo.name%>" /> &nbsp;唯一标识符，使用英文，模板接入该值。</td>
				</tr>
				<tr>
					<th>标题：</th>
					<td><input type="text" id="title" value="<%=@codeinfo.title%>" /> &nbsp;使用中文标识，便于识别。</td>
				</tr>
				<tr>
					<th>内容：</th>
					<td><textarea id="content"><%=@codeinfo.content%></textarea></td>
				</tr>
			</table>
		
			<div id="codebutton">
				<input type="button" id="btnSave" value="保 存" /> &nbsp;
				<input type="reset" value="重 置" /> &nbsp;
				<% if(@codeinfo.id != nil ) %><input type="button" id="btnDelete" value="删 除" /><% end %>
			</div>
		</form>
	</div>
</div>

<script type="text/javascript">
$(document).ready(function(){
	$('#btnSave').click(btnSave_click);
	$('#btnDelete').click(btnDelete_click);

	$.ajaxSetup({  
	    beforeSend: function(xhr) {  
	        xhr.setRequestHeader("X-CSRF-Token", $("meta[name=\"csrf-token\"]").attr("content"));  
	    }  
	});	
});

// 保存事件
function btnSave_click() {
	var name = $('#name').val();
	var title = $('#title').val();
	var content = $('#content').val();
	if(name.length == 0 && title.length == 0) {
		alert('表单填写不完整');
		return;
	}

	// 提交保存事件
	$.ajax({
		type : 'POST',
		url : location.href,
		data : { id:'<%=@codeinfo.id%>', 'name' : name, 'title' : title, 'content' : content },
		success: function(d){
			if(d == 1) {
				alert('保存成功。');
				location.href = location.href;
			} else {
				alert('保存失败，请重新尝试。');
			}
		}
	});
}

// 删除事件
function btnDelete_click() {
	if(confirm('确定要删除吗?')) {
		$.ajax({
		type : 'POST',
		url : '/admin/codeparter/delete',
		data : { id:'<%=@codeinfo.id%>' },
		success: function(d){
			if(d == 1) {
				alert('删除成功。');
				location.href = '/admin/codeparter';
			} else {
				alert('删除失败，请重新尝试。');
			}
		}
	});
	}
}
</script>



